/*******************************************************************************

This code file calculates building-level 421-a incentives.

*******************************************************************************/

	** Merge in tax rates
	
		merge m:1 yearpermit using "$data/raw/tax_schedule/taxrates_byclassyear.dta", nogen keep(1 3)
		
	** Merge in distortion from taxable value caps
	
		merge m:1 bbl using "$data/raw/assessments/av_fraction_history_wide.dta", nogen keep(1 3)
	
	** Merge in timetable for deduction phaseout
	
		gen tmp = 1
		merge m:1 tmp using "$data/raw/tax_schedule/deduction_phaseout_wide.dta", nogen keep(1 3)
		drop tmp
		
	** Merge in underassessment of condos
	
		merge m:1 bbl using "$data/raw/assessments/underassessment.dta", nogen keep(1 3)
	
	** Calculate share of property value not exempted due to AV cap
	
		gen avcap = 0
		replace avcap = 65000*1.03^(yearpermit-2008) if yearpermit >= 2008
		gen shval_capped = max(0,((assesstot - assessland)/unitstotal - avcap)/((assesstot - assessland)/unitstotal))
		drop avcap
		
	** Construct PDV tax benefit of 421a
					
		local beta = $beta
				
		forvalues l = 10(5)25 {
			
			gen dfactor_`l' = 0
			
			forvalues y = 0/25 {
				replace dfactor_`l' = dfactor_`l' + `beta'^`y'*fraction_pred`y'*(1-deduct`l'_`y')
			}
			
		}	
		
		gen dfactor_0 = 0
		forvalues y = 0/25 {
			replace dfactor_0 = dfactor_0 + `beta'^`y'*fraction_pred`y'
		}
		
		capture drop dtaxrate_* taxrate
				
		gen taxrate = .
		replace taxrate = taxrate_class1 if unitstotal == 3
		replace taxrate = taxrate_class2 if unitstotal > 3
	
		gen dfactor421a_onsite = .
		replace dfactor421a_onsite = dfactor_20 if gea == 1 & yearpermit <= 2007
		replace dfactor421a_onsite = dfactor_25 if gea == 1 & yearpermit > 2007
		replace dfactor421a_onsite = dfactor_25 if gea == 0 
		
		gen dfactor421a_offsite = .
		replace dfactor421a_offsite = dfactor_10 if gea == 1 & yearpermit < 2008
		replace dfactor421a_offsite = dfactor_15 if gea == 0 & yearpermit < 2008
		replace dfactor421a_offsite = dfactor_15 if gw_gea == 1 & yearpermit < 2008
		replace dfactor421a_offsite = 0 if yearpermit >= 2008
		
		gen dfactor421a_none = .
		replace dfactor421a_none = dfactor_0 if gea == 1 | gw_gea == 1
		replace dfactor421a_none = dfactor_15 if gea == 0 & sub_gea == 0 & yearpermit < 2008
		replace dfactor421a_none = dfactor_10 if gea == 0 & sub_gea == 1 & yearpermit < 2008
		replace dfactor421a_none = shval_capped*dfactor_0 + (1-shval_capped)*dfactor_15 if gea == 0 & yearpermit >= 2008
						
		gen dtaxrate_onsite = (dfactor421a_none - dfactor421a_onsite) * taxrate
		
		gen dtaxrate_offsite = (dfactor421a_none - dfactor421a_offsite) * taxrate
	
	** Neighborhood Preservation Program
	replace dtaxrate_onsite = 0 if (npp == 1 & yearpermit < 2008) 
	
	** Buildings with less than 5 units don't have to build inclusionary housing
	replace dtaxrate_onsite = 0 if unitsres < 5
		
	** Condo underassessment
		
		bys nta: gegen underassess_m = mean(underassess)
		replace underassess = underassess_m if missing(underassess) & condo == 1
		replace underassess = 1 if condo == 0
		
		summ underassess
		replace underassess = r(mean) if missing(underassess) & condo == 1 
			
		foreach dtaxrate of varlist dtaxrate_onsite dfactor421a_offsite {
			replace `dtaxrate' = `dtaxrate'*underassess if !missing(underassess)
		}
		
		drop underassess_m
	
	** Commerical floor area adjustment	
	
		capture drop excess_nonres
		
		gen excess_nonres = max((bldgarea-resarea)/bldgarea - 0.12,0)
		replace dtaxrate_onsite = dtaxrate_onsite*(1-excess_nonres)
				
		gen taxrate_ = .
		replace taxrate_ = (1-`beta') * ((1-excess_nonres)*dfactor421a_onsite + excess_nonres*dfactor421a_none) * taxrate if inclusionary_onsite == 1
		replace taxrate_ = (1-`beta') * ((1-excess_nonres)*dfactor421a_offsite + excess_nonres*dfactor421a_none) * taxrate if inclusionary_offsite == 1
		replace taxrate_ = (1-`beta') * dfactor421a_none * taxrate if max(inclusionary_onsite,inclusionary_offsite) == 0

		drop taxrate
		rename taxrate_ taxrate
		
		drop excess_nonres
		
	** Other exemptions

		capture drop nonexemptshare
		
		gen nonexemptshare = max(0,min(1,1 - exempttot/(assesstot*sh_taxable)))
		replace dtaxrate_onsite = dtaxrate_onsite*nonexemptshare if receipt_421a == 0
		replace dtaxrate_offsite = dtaxrate_offsite*nonexemptshare if receipt_421a == 0
		
		replace taxrate = nonexemptshare * taxrate if receipt_421a == 0
		drop nonexemptshare 
		
	drop dfactor421a_onsite dfactor* shval_capped taxrate_class1 taxrate_class2 fraction_pred* deduct*  
